package com.cyf.db.mapper;

import com.cyf.db.bean.MerchantCoupon;
import com.cyf.merchat.network.bean.MerchantCouponVo;
import com.cyf.merchat.network.bean.UserIssueRecordReq;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 商户生成的优惠券
 * Created by cage on 2017/3/30.
 */

public interface MerchantCouponMapper {

    @Insert("insert into merchant_coupon(merchantId, parkingId, mobile, appUserId, plate, createdTime, endTime, status, " +
            "sendToParking, couponCode, couponType, freeHours, freeAmount, feeGroupId,bindingFeeGroupId, " +
            "freeLimit, limitParkingId, useWay, type, discount,localParkingId) " +
            "values(#{merchantCoupon.merchantId}, #{merchantCoupon.parkingId}, #{merchantCoupon.mobile}, #{merchantCoupon.appUserId}, " +
            "#{merchantCoupon.plate}, #{merchantCoupon.createdTime}, #{merchantCoupon.endTime}, #{merchantCoupon.status}, " +
            "#{merchantCoupon.sendToParking}, #{merchantCoupon.couponCode}, #{merchantCoupon.couponType}, " +
            "#{merchantCoupon.freeHours}, #{merchantCoupon.freeAmount}, #{merchantCoupon.feeGroupId},#{merchantCoupon.bindingFeeGroupId}," +
            "#{merchantCoupon.freeLimit}, #{merchantCoupon.limitParkingId}, #{merchantCoupon.useWay}, #{merchantCoupon.type}, #{merchantCoupon.discount}," +
            "#{merchantCoupon.localParkingId})")
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "merchantCoupon.id", before = false, resultType = int.class)
    void insertMerchantCoupon(@Param("merchantCoupon") MerchantCoupon merchantCoupon);

    @Select("select * from merchant_coupon where appUserId = #{appUserId} and status = #{status}")
    List<MerchantCoupon> getByAppUserIdAndStatus(@Param("appUserId") Integer id, @Param("status") int status);

    @Select("select * from merchant_coupon where appUserId = #{appUserId} and status != 1")
    List<MerchantCoupon> getUnUsedByAppUserId(@Param("appUserId") Integer id);

    // parkingId!=1为车场券
    @Select("select * from merchant_coupon where appUserId = #{appUserId} and status = #{status} and parkingId != 1")
    List<MerchantCoupon> getParkingCouponByAppUserIdAndStatus(@Param("appUserId") Integer id, @Param("status") int status);

    // parkingId=1为车易付券
    @Select("select * from merchant_coupon where appUserId = #{appUserId} and status = #{status} and parkingId = 1")
    List<MerchantCoupon> getCyfCouponByAppUserIdAndStatus(@Param("appUserId") Integer id, @Param("status") int unused);

    @Select("select count(id) from merchant_coupon where merchantId = #{merchantId} and createdTime >= #{date}")
    Integer getCountByMerchantIdAndDate(@Param("merchantId") Integer merchantId, @Param("date") Date date);

    @Select("select count(id) from merchant_coupon where merchantId = #{merchantId}")
    Integer getCountByMerchantId(@Param("merchantId") Integer merchantId);

    @Update("update merchant_coupon set sendToParking = #{sendToParking},localMerchantCouponId=#{localMerchantCouponId} " +
            "where id = #{id}")
    void updateSendParkingStatus(@Param("id") Integer id, @Param("localMerchantCouponId") Integer localMerchantCouponId, @Param("sendToParking") Integer sendToParking);

    @Select("select count(id) from merchant_coupon where plate = #{plate} and parkingId=#{parkingId} and status=1")
    int getCountByPlate(@Param("plate") String plate, @Param("parkingId") int parkingId);


    @Select("<script>" +
            "select * from merchant_coupon where parkingId=#{parkingId} and" +
            " merchantId=#{merchantId} " +
            "<if test=\"status!=null and status !=-1 \">" +
            " and status=#{status} " +
            "</if>" +
            "<if test=\"endTime!=null and endTime != '' \">" +
            "<![CDATA[ and endTime>='${endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"createdTime!=null and createdTime != '' \">" +
            "<![CDATA[ and createdTime>='${createdTime } 00:00:00' ]]>" +
            "<![CDATA[ and createdTime<='${createdTime } 23:59:59' ]]>" +
            "</if>" +
            " ORDER BY createdTime DESC" +
            "</script>")
    List<MerchantCoupon> getCountByMerchantIdAndParkingId(@Param("parkingId") int parkingId,
                                                          @Param("merchantId") Integer merchantId,
                                                          @Param("status") int status,
                                                          @Param("createdTime") String createdTime,
                                                          @Param("endTime") String endTime);

    @Insert("<script>" +
            "insert into merchant_coupon(merchantId, parkingId, mobile, appUserId, plate, createdTime, endTime, status, " +
            "sendToParking, couponCode, couponType, freeHours, freeAmount, feeGroupId,bindingFeeGroupId, " +
            "freeLimit, limitParkingId, useWay, type, discount,carName,remark,localParkingId) " +
            "values" +
            "  <foreach collection=\"list\" item=\"item\" index=\"index\" " +
            "   separator=\",\"> " +
            "(#{item.merchantId}, #{item.parkingId}, #{item.mobile}, #{item.appUserId}, " +
            "#{item.plate}, #{item.createdTime}, #{item.endTime}, #{item.status}, " +
            "#{item.sendToParking}, #{item.couponCode}, #{item.couponType}, " +
            "#{item.freeHours}, #{item.freeAmount}, #{item.feeGroupId},#{item.bindingFeeGroupId}," +
            "#{item.freeLimit}, #{item.limitParkingId}, #{item.useWay}, #{item.type}, #{item.discount}," +
            "#{item.carName},#{item.remark},#{item.localParkingId})" +
            "  </foreach>" +
            "</script>")
    @Options(useGeneratedKeys = true,keyProperty = "id")
    void insertMerchantCouponList(@Param("list") List<MerchantCoupon> merchantCoupon);

    @Select("<script>" +
            "select count(*) from merchant_coupon mc " +
            "left join parking p on p.id = mc.parkingId " +
            "where mc.parkingId=#{req.parkingId} and mc.status = 1 and mc.useWay = 2 " +
            "and mc.couponType = 2 " +
            "<if test=\"req.endTime != null and req.endTime != '' \">" +
            "<![CDATA[ and mc.endTime>='${req.endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"req.phone != null and req.phone != '' \">" +
            " and  mc.mobile = #{req.phone} " +
            "</if>" +
            "<if test=\"req.plate != null and req.plate != '' \">" +
            " and  mc.plate = #{req.plate} " +
            "</if>" +
            " ORDER BY mc.createdTime DESC" +
            "</script>")
    int countMerchantCoupon(@Param("req") UserIssueRecordReq req);

    @Select("<script>" +
            "select mc.*,p.name as parkingName from merchant_coupon mc " +
            "left join parking p on p.id = mc.parkingId " +
            "where mc.parkingId=#{req.parkingId} and mc.status = 1 and mc.useWay = 2 " +
            "and mc.couponType = 2 " +
            "<if test=\"req.endTime != null and req.endTime != '' \">" +
            "<![CDATA[ and mc.endTime>='${req.endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"req.phone != null and req.phone != '' \">" +
            " and  mc.mobile = #{req.phone} " +
            "</if>" +
            "<if test=\"req.plate != null and req.plate != '' \">" +
            " and  mc.plate = #{req.plate} " +
            "</if>" +
            " ORDER BY mc.createdTime DESC" +
            "</script>")
    List<MerchantCoupon> selectByMobile(@Param("req") UserIssueRecordReq req);

    @Select("<script>" +
            "select mc.*,p.name as parkingName from merchant_coupon mc " +
            "left join parking p on p.id = mc.parkingId" +
            "<where> " +
            "<if test=\"status != null and status != -1 \">" +
            " and mc.status=#{status} " +
            "</if>" +
            "<if test=\"endTime != null and endTime != '' \">" +
            "<![CDATA[ and mc.endTime>='${endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"mobile != null and mobile != ''\">" +
            " and  mc.mobile = #{mobile} " +
            "</if> " +
            " and mc.useWay = 2" +
            "</where>" +
            " ORDER BY mc.createdTime DESC" +
            "</script>")
    List<MerchantCoupon> selectCouponByMobile(@Param("mobile") String mobile, @Param("endTime") String endTime,
                                              @Param("status") int status);

    @Select("<script>" +
            "select count(*) from merchant_coupon " +
            "where parkingId=#{parkingId} and " +
            " merchantId=#{merchantId} " +
            "<if test=\"endTime!=null and endTime != '' \">" +
            "<![CDATA[ and endTime>='${endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"status != null and status !=-1 \">" +
            " and status=#{status} " +
            "</if>" +
            "</script>")
    int getCountCoupon(@Param("parkingId") int parkingId,
                       @Param("merchantId") Integer merchantId,
                       @Param("endTime") String endTime,
                       @Param("status") int status);

    @Update("update merchant_coupon set status = 0,timespent = #{timespent},sendToParking = 0," +
            "shouldFee = #{shouldFee},couponFee = #{couponFee},actualFee = #{actualFee}  where id = #{id}")
    void updateCoupon(@Param("id") Integer id, @Param("timespent") Date timespent, @Param("shouldFee") Integer shouldFee, @Param("couponFee") Integer couponFee, @Param("actualFee") Integer actualFee);





    /*@Update("update merchant_coupon set status = 0,timespent = #{timespent},sendToParking = 0  where id = #{id}" )
    void updateCouponFee(@Param("id") Integer id,@Param("timespent") Date timespent);
*/


    @Select("<script>" +
            "select * from  merchant_coupon  " +
            "  <where> " +
            "   <if test=\" ids != null and ids.size() > 0\"> " +
            "    <foreach collection=\"ids\" item=\"id\" index=\"index\" " +
            "     separator=\",\" open=\"id in (\" close=\")\"> " +
            "     (#{id}) " +
            "    </foreach> " +
            "   </if> " +
            "   and status = 1" +
            "  </where>" +
            "</script>")
    List<MerchantCoupon> selectByIds(@Param("ids") List<Integer> ids);

    @Select("<script>" +
            "select count(*) from merchant_coupon mc " +
            "<where> " +
            "<if test=\"status != null and status != -1 \">" +
            " and mc.status=#{status} " +
            "</if>" +
            "<if test=\"endTime != null and endTime != '' \">" +
            "<![CDATA[ and mc.endTime>='${endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"mobile != null and mobile != ''\">" +
            " and  mc.mobile = #{mobile} " +
            "</if>" +
            " and mc.useWay = 2 " +
            "</where>" +
            "</script>")
    int countCouponTotal(@Param("mobile") String mobile, @Param("endTime") String endTime,
                         @Param("status") int status);

    @Select("<script>" +
            "select count(*) from merchant_coupon mc " +
            "<where> " +
            "status = 0" +
            "<if test=\"endTime != null and endTime != '' \">" +
            "<![CDATA[ and mc.endTime>='${endTime} 00:00:00' ]]>" +
            "</if>" +
            "<if test=\"mobile != null and mobile != ''\">" +
            " and  mc.mobile = #{mobile} " +
            "</if>" +
            "</where>" +
            "</script>")
    int countCouponUse(@Param("mobile") String mobile, @Param("endTime") String endTime);

    @Update("update merchant_coupon set status = 3,sendToParking = 0 where id = #{id}")
    void updateMerchantStatus(@Param("id") Integer id);

    @Select("select * from merchant_coupon where id = #{id} and status = 1 ")
    MerchantCoupon getById(@Param("id") Integer id);

    @Update("update merchant_coupon set mobile = #{mobile} where id = #{id}")
    void updateMerchantByMobile(@Param("id") Integer id, @Param("mobile") String mobile);

    @Select("SELECT createdTime FROM local_rechargerecord WHERE parkingId = #{parkingId}  AND merchantId = #{merchantId} ORDER BY createdTime DESC LIMIT 0,1")
    Date selectMerchntRechargeTime(@Param("parkingId") Integer parkingId, @Param("merchantId") Integer merchantId);

    @Select("select * from merchant_coupon where id = #{id} ")
    MerchantCoupon merchantCouponById(@Param("id") Integer id);

    @Update("update merchant_coupon set status=1,shouldFee = null,couponFee = null,actualFee = null,sendToParking = 0 where id = #{ids}")
    void updateCouponById(@Param("ids") Integer ids);

    @Select(" SELECT sum(shouldFee) shouldFee,sum(couponFee) couponFee,sum(actualFee) actualFee,sum(freeAmount) as freeAmount  FROM merchant_coupon AS co " +
            " LEFT JOIN local_merchant_info AS fo ON co.merchantId = fo.id  " +
            " WHERE fo.parkingId = #{parkingId} AND fo.id = #{merchantId} AND (fo.merchantType = 2 or fo.merchantType = 3 ) ")
    MerchantCouponVo selectMerchantCouponTotal(@Param("parkingId") Integer parkingId,@Param("merchantId") Integer merchantId);

}
